/*
 * @Author: xuman
 * @Date: 2020-06-15 14:34:52
 * @LastEditors: xuman
 * @LastEditTime: 2020-06-15 15:54:31
 */
// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts')
// 引入柱状图
require('echarts/lib/chart/line')
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')
require('echarts/lib/component/grid')

export default class LineChart {
  private myChart: any
  private option: any = ([xAxisData, yAxisData]: any) => ({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    xAxis: {
      type: 'category',
      data: xAxisData
    },
    yAxis: {
      name: this.myChart._dom.id,
      type: 'value'
    },
    series: [
      {
        data: yAxisData,
        type: 'line',
        smooth: true
      }
    ]
  })
  constructor(dom: any) {
    this.myChart = echarts.init(document.getElementById(dom))
  }
  public setOption(data: any[]) {
    const tempData = [
      data.map(item => item.xaxis),
      data.map(item => item.yaxis)
    ]
    this.myChart && this.myChart.setOption(this.option(tempData))
  }
}
